package com.chwn.curiousdiary.ui.other;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.ui.main.MainActivity;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.User;
import com.chwn.curiousdiary.ui.login.LoginActivity;
import com.chwn.curiousdiary.utils.SPUtils;
import com.gyf.barlibrary.BarHide;
import com.gyf.barlibrary.ImmersionBar;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.List;

/**
 * Created by Myshikin on 2017/3/20.
 */
public class SplashActivity extends AppCompatActivity implements  PermissionListener {


    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                initPerssion();
            }
        }
    };

    private void enterNextPage() {
        if (!SPUtils.getBooleanSP("ISFIRSTINGOAPP", false)) {
            startActivity(new Intent(SplashActivity.this, GuideActivity.class));
            finish();
            return;
        }

        User user = DiaryApplication.mInstance.getmUser();

        if (user != null && !TextUtils.isEmpty(user.getBabybirth()) && Integer.parseInt(user.getIsBind()) == 1) {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);

        } else {
            Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
            startActivity(intent);
        }
        finish();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏
        if (ImmersionBar.hasNavigationBar(this))
            ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).init();
        super.onCreate(savedInstanceState);
        handler.sendEmptyMessageDelayed(1, Constant.SPLASH_TIME);
        /*Intent intent = new Intent(this, AudioService.class);
        intent.putExtra("type", 6);
        startService(intent);*/
    }

    private void initPerssion() {
        if (AndPermission.hasPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)) {
            enterNextPage();
        } else {
            AndPermission.with(this)
                    .requestCode(Constant.EXTERNAL_STORAGE)
                    .permission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                    .rationale(new RationaleListener() {
                                   @Override
                                   public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                                       AndPermission.rationaleDialog(SplashActivity.this, rationale).show();
                                   }
                               }
                    )
                    .send();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);


        AndPermission.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onSucceed(int requestCode, List<String> grantPermissions) {
        enterNextPage();
    }

    @Override
    public void onFailed(int requestCode, List<String> deniedPermissions) {
        enterNextPage();
    }
}
